package cc.vant.wechat.model;

import android.app.Activity;
import android.util.Log;

import cc.vant.wechat.model.packet.FailPacket;
import cc.vant.wechat.model.packet.SimpleTextPacket;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class LoginSignupInHandler extends SimpleChannelInboundHandler<ByteBuf> {
    private static final String TAG = "LoginSignupInHandler";

    private Activity activity;

    public LoginSignupInHandler(Activity activity) {
        this.activity = activity;
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        Log.d(TAG, "channelRead0: 接受到消息");

        int type = msg.getInt(0);
        if (type == FailPacket.TYPE) {
            //登录失败

        } else {
            SimpleTextPacket textPacket = new SimpleTextPacket();
            textPacket.unserialize(msg);
            String session = textPacket.getText();
            MyApplication application = (MyApplication) activity.getApplication();
            application.setSession(session);
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}